<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Hook.js - Documentation</title>

    <script src="scripts/prettify/prettify.js"></script>
    <script src="scripts/prettify/lang-css.js"></script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc.css">
</head>
<body>

<input type="checkbox" id="nav-trigger" class="nav-trigger" />
<label for="nav-trigger" class="navicon-button x">
  <div class="navicon"></div>
</label>

<label for="nav-trigger" class="overlay"></label>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Externals</h3><ul><li><a href="external-EventEmitter.html">EventEmitter</a></li></ul><h3>Events</h3><ul><li><a href="external-EventEmitter.html#event:error">error</a></li></ul><h3>Namespaces</h3><ul><li><a href="Express.html">Express</a><ul class='methods'><li data-type='method'><a href="Express.html#.Router">Router</a></li></ul></li></ul><h3>Global</h3><ul><li><a href="global.html#exports">exports</a></li></ul>
</nav>

<div id="main">

    <h1 class="page-title">Hook.js</h1>

    <section>
        <article>
            <pre class="prettyprint source linenums"><code>'use strict';
/**
 * Event Emitter
 *
 * @external EventEmitter
 *
 * @see {@link https://nodejs.org/dist/latest-v6.x/docs/api/events.html#events_class_eventemitter|Node.JS - Event module}
 */
const EventEmitter = require('events'),
      Crypto = require('crypto'),
      SSE = require('express-sse');


function channel(source, filter) {

    var target = new SSE();

    return  function (request, response, next) {

        if (! request.accepts('text/event-stream'))  return next();

        target.init(request, response);

        function send(event) {

            if ( filter.call(this, event, request) ) {

                target.send(event, event.action);

                target.send( event );
            }
        }

        source.on('*', send);

        request.on('close',  function () {

            source.removeListener('*', send);
        });
    };
}


module.exports = function (config) {

    const emitter = new EventEmitter();

    /**
     * @api {post} /hookHub  Receive all kinds of Event from a Web Hook
     *
     * @apiName    postEvent
     * @apiVersion 0.5.0
     * @apiGroup   Event
     *
     * @apiHeader {String} X-GitHub-Event     Event name
     * @apiHeader {String} X-GitHub-Delivery  Unique ID for this delivery
     * @apiHeader {String} X-Hub-Signature    HMAC hex digest of the payload
     *
     * @apiParam {String}   action         Event name
     * @apiParam {Object}   [repository]
     * @apiParam {Object}   [organization]
     */
    this.post('/hookHub',  function (request, response) {

        var data = JSON.stringify( request.body ),
            sign = (request.get('X-Hub-Signature') || '').split('=');

        request.body.id = request.get('X-GitHub-Delivery');

        request.body.sign = sign;

        if (
            sign[0] &amp;&amp; (
                sign[1] !==
                Crypto.createHmac(sign[0], config.HookSecret)
                    .update( data ).digest('hex')
            )
        ) {
            /**
             * Signature verification failed
             *
             * @event external:EventEmitter#error
             *
             * @type {object}
             *
             * @property {string}   id    `X-GitHub-Delivery` header
             * @property {string[]} sign  Array splited from
             *                            `X-Hub-Signature` header
             *
             * @see {@link https://developer.github.com/webhooks/#payloads}
             */
            emitter.emit('error', request.body);

            return  response.status( 400 ).end('Signature verification failed');
        }

        emitter.emit(request.get('X-GitHub-Event'), request.body);

        emitter.emit('*', request.body);

        response.end();
    });

    /**
     * @api {get} /orgs/:org/events  Organization SSE
     *
     * @apiName    getOrgEvent
     * @apiVersion 0.5.1
     * @apiGroup   Event
     *
     * @apiHeader {String} Accept=text/event-stream
     *
     * @apiParam {String} org
     */
    this.get('/orgs/:org/events',  channel(emitter,  function (event, request) {

        return  ((event.organization || '').login  ===  request.params.org);
    }));

    /**
     * @api {get} /repos/:owner/:repo/events  Repository SSE
     *
     * @apiName    getRepoEvent
     * @apiVersion 0.5.1
     * @apiGroup   Event
     *
     * @apiHeader {String} Accept=text/event-stream
     *
     * @apiParam {String} owner
     * @apiParam {String} repo
     */
    this.get('/repos/:owner/:repo/events', channel(
        emitter,  function (event, request) {

            return (
                (event.repository || '').full_name  ===
                request.params.owner + '/' + request.params.repo
            );
        }
    ));

    /**
     * @api {get} /repos/:owner/:repo/issues/events  Repository issue SSE
     *
     * @apiName    getIssueEvent
     * @apiVersion 0.5.1
     * @apiGroup   Event
     *
     * @apiHeader {String} Accept=text/event-stream
     *
     * @apiParam {String} owner
     * @apiParam {String} repo
     */
    this.get('/repos/:owner/:repo/issues/events', channel(
        emitter,  function (event, request) {

            return  event.issue &amp;&amp; (
                (event.repository || '').full_name  ===
                request.params.owner + '/' + request.params.repo
            );
        }
    ));

    return emitter;
};
</code></pre>
        </article>
    </section>

</div>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> using the <a href="https://github.com/clenemt/docdash">docdash</a> theme.
</footer>

<script>prettyPrint();</script>
<script src="scripts/linenumber.js"></script>
</body>
</html>
